<script>
let userStorageKey = 'YangFan-SystemCurrUser'
export default {
  // 账号
  Username: '',
  // 头像
  Icon: './static/icon.png',
  // 身份令牌
  AccessToken: '',
  // 刷新令牌
  RefreshToken: '',
  // token类型
  TokenType: '',
  // token有效期
  ExpiresIn: 0,

  // 检查用户缓存状态(页面刷新的时候)
  checkCache: function () {
    var localString = localStorage.getItem(userStorageKey)
    if (typeof localString !== 'string') {
      return false
    }
    if (localString === '' || localString === null) {
      return false
    }
    var obj = JSON.parse(localString)
    this.TokenType = obj.tokenType
    this.ExpiresIn = obj.expireTime
    this.AccessToken = obj.token
    this.RefreshToken = obj.reflushToken
    this.Username = obj.username
  },

  // 设置用户状态
  setCache: function (data) {
    if (data === null) {
      localStorage.removeItem(userStorageKey)
      this.AccessToken = ''
      this.Username = ''
      this.RefreshToken = ''
      return
    }
    localStorage.setItem(userStorageKey, JSON.stringify(data))
    this.checkCache()
  },

  getToken:function () {
    var localString = localStorage.getItem(userStorageKey)
    // console.log('localString',localString)
    if (!localString){
      return ''
    }
    var obj = JSON.parse(localString)
    return obj.token
  },

  // 检查是否登录
  IsLogin: function () {
    this.checkCache()
    return typeof this.AccessToken === 'string' && this.AccessToken.length > 5
  }
}
</script>
